<template>
  <a-modal
    title="订单审核"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    
    <a-form-model ref="form" :model="model" :rules="validatorRules" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
      
      <a-form-model-item label="订单编号">
        <a-input v-model="orderInfo.orderNo" :disabled="true"></a-input>
      </a-form-model-item>

      <a-form-model-item label="客户名称">
        <a-input v-model="orderInfo.customerName" :disabled="true"></a-input>
      </a-form-model-item>

      <a-form-model-item label="订单金额">
        <a-input v-model="orderAmountText" :disabled="true"></a-input>
      </a-form-model-item>

      <a-form-model-item label="审核结果" prop="auditStatus">
        <a-radio-group v-model="model.auditStatus">
          <a-radio :value="1">审核通过</a-radio>
          <a-radio :value="9">审核驳回</a-radio>
        </a-radio-group>
      </a-form-model-item>

      <a-form-model-item label="审核意见" prop="auditComment">
        <a-textarea
          v-model="model.auditComment"
          placeholder="请输入审核意见"
          :rows="4"/>
      </a-form-model-item>

    </a-form-model>
  </a-modal>
</template>

<script>
  import { auditSemiProductSalesOrder } from '@/api/oms/semiProductSalesOrder'

  export default {
    name: 'AuditModal',
    data() {
      return {
        visible: false,
        confirmLoading: false,
        orderInfo: {},
        model: {},
        validatorRules: {
          auditStatus: [
            { required: true, message: '请选择审核结果!' }
          ],
          auditComment: [
            { required: true, message: '请输入审核意见!' }
          ]
        }
      }
    },
    computed: {
      orderAmountText() {
        return this.orderInfo.totalAmount ? `¥${this.orderInfo.totalAmount.toFixed(2)}` : '¥0.00';
      }
    },
    methods: {
      showAudit(record) {
        this.visible = true;
        this.orderInfo = record;
        this.model = {
          auditStatus: null,
          auditComment: ''
        };
      },
      
      handleOk() {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            this.confirmLoading = true;
            try {
              const res = await auditSemiProductSalesOrder(
                this.orderInfo.id,
                this.model.auditStatus,
                this.model.auditComment
              );
              
              if (res.success) {
                this.$message.success(res.message);
                this.$emit('ok');
                this.visible = false;
              } else {
                this.$message.warning(res.message);
              }
            } catch (error) {
              this.$message.error('审核失败');
              console.error('审核失败:', error);
            } finally {
              this.confirmLoading = false;
            }
          }
        });
      },
      
      handleCancel() {
        this.visible = false;
      }
    }
  }
</script>
